package com.qianfeng.zhangwei.volley;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.qianfeng.zhangwei.volley.bean.SupperRecommend;
import com.qianfeng.zhangwei.volley.request.GsonRequest;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Volley简单使用
 */
public class VolleySmipleActivity extends ActionBarActivity {
    private static final String GET_URL = "http://mobapi.meilishuo.com/2.0/express/recommend_entrance?imei=000000000000000&mac=08%3A00%3A27%3A51%3A2e%3Aaa&qudaoid=11601&access_token=6c5428735c0a46cbd4bd0ac53fd9da30&st=1429002510&_sign=bf62212d98e50df072288c9e86077939467f9b10";
    private static final String POST_URL = "http://mobapi.meilishuo.com/2.0/express/recommend_entrance";
    private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley_smaple);
        queue = Volley.newRequestQueue(this);
    }

    /**
     * 网络获取json数据
     * 第一步 创建请求队列
     * 第二步 创建请求
     * 第三步 设置tag
     * 第四步 将请求添加进队列
     * 第五步 onstop方法中取消请求队列
     */

    public void stringRequestMethod() {
        /**
         * 代码执行顺序
         *  1.StringRequest request = new StringRequest(参数);
         *  2.request.setTag(this);
         *  3.queue.add(request);
         *  4.请求成功 onResponse回调方法
         *    请求失败 onErrorResponse回调方法
         */

        /**
         * 参数一 请求方式 Method.Get,Method.Post 默认get请求
         * 参数二 请求数据的url
         * 参数三 请求成功的接口回调
         * 参数四 请求失败的接口回调
         */
        //StringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener);

        StringRequest request = new StringRequest(GET_URL, new Response.Listener<String>() {
            /**
             * 请求数据成功的接口回调
             * @param s
             */
            @Override
            public void onResponse(String s) {
                //主线程中可以直接操作UI
                Log.e("", s);
            }
            /**
             * 请求失败接口回调
             */
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

        //取消请求的tag 单次请求直接设置当前的Activity 多次请求请动态设置tag以免框架直接从本地读取数据
//        request.setTag(this);


    }

    /**
     * 步骤同上
     */
    private void sqRequestPostMethod() {
        StringRequest request = new StringRequest(Request.Method.POST, POST_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("", s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("", volleyError.getMessage());
            }
        }) {
            /**
             * 重写getParams传递参数
             * @return
             * @throws AuthFailureError
             */

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<>();
                params.put("imei", "000000000000000");
                params.put("mac", "08%3A00%3A27%3A51%3A2e%3Aaa");
                params.put("qudaoid", "11601");
                params.put("access_token", "6c5428735c0a46cbd4bd0ac53fd9da30");
                params.put("st", "1429002510");
                params.put("_sign", "bf62212d98e50df072288c9e86077939467f9b10");
                return params;
            }
        };
    }

    private void jsonObjectRequest() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, POST_URL, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        request.setTag(this);
        queue.add(request);
    }

    /**
     * 自定义Request
     */
    private void gsonRequestRequest() {
        HashMap<String, String> params = new HashMap<>();
        params.put("imei", "000000000000000");
        params.put("mac", "08%3A00%3A27%3A51%3A2e%3Aaa");
        params.put("qudaoid", "11601");
        params.put("access_token", "6c5428735c0a46cbd4bd0ac53fd9da30");
        params.put("st", "1429002510");
        params.put("_sign", "bf62212d98e50df072288c9e86077939467f9b10");
//        GsonRequest<SupperRecommend> request = new GsonRequest<SupperRecommend>(Request.Method.POST, POST_URL, SupperRecommend.class, params, new Response.Listener<SupperRecommend>() {
//            @Override
//            public void onResponse(SupperRecommend supperRecommend) {
//                if (supperRecommend != null) {
//                    List<Recommend> list = supperRecommend.getData();
//                }
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//            }
//        });

        GsonRequest request = new GsonRequest(Request.Method.POST,POST_URL,SupperRecommend.class,params, new Response.Listener<SupperRecommend>() {
            @Override
            public void onResponse(SupperRecommend response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        queue.add(request);
    }

    /**
     *
     */
    @Override
    protected void onStop() {
        super.onStop();
    }

}
